from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from apps.app01.models import User
from apps.app01.views.user_info.serializer.user import UserSerializer


class UserDetailView(APIView):
    # authentication_classes = []

    def get(self, request):
        # 从 token 中获取用户 ID
        user_id = request.user['user_id']
        try:
            # 查找用户信息
            user = User.objects.get(id=user_id)
        except User.DoesNotExist:
            return Response({"detail": "User not found."}, status=404)

        # 使用序列化器将用户数据转化为 JSON 格式
        serializer = UserSerializer(user)

        return Response(serializer.data)
# class UserDetailView(APIView):
#     authentication_classes = []
#     def get(self, request):
#         # 从 token 中获取用户 ID
#         user = request.user  # Assuming request.user is a User instance
#
#         try:
#             # 查找用户信息
#             user_instance = User.objects.get(id=user.id)
#         except User.DoesNotExist:
#             return Response(
#                 {
#                     "code": 404,
#                     "message": "User not found.",
#                     "data": None
#                 },
#                 status=404
#             )
#
#         # 使用序列化器将用户数据转化为 JSON 格式
#         serializer = UserSerializer(user_instance)
#
#         return Response(
#             {
#                 "code": 200,
#                 "message": "Success",
#                 "data": serializer.data
#             },
#             status=200
#         )